/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import Control.Constantes;
import Control.Controlador;
import java.awt.Color;

/**
 *
 * @author Leonardo Bohórquez
 */
public class FramePrincipal extends javax.swing.JFrame {

    /**
     * Creates new form FramePrincipal
     */
    public FramePrincipal() {
	initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtFieldB = new javax.swing.JTextField();
        txtFieldA = new javax.swing.JTextField();
        cmdAdd = new javax.swing.JButton();
        cmdSubs = new javax.swing.JButton();
        cmdMult = new javax.swing.JButton();
        cmdDiv = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        lblAns = new javax.swing.JLabel();
        cmdClear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Calculadora");
        setResizable(false);

        txtFieldB.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        txtFieldB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtFieldBActionPerformed(evt);
            }
        });

        txtFieldA.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N

        cmdAdd.setText("Sumar");
        cmdAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAddActionPerformed(evt);
            }
        });

        cmdSubs.setText("Restar");
        cmdSubs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSubsActionPerformed(evt);
            }
        });

        cmdMult.setText("Multiplicar");
        cmdMult.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdMultActionPerformed(evt);
            }
        });

        cmdDiv.setText("Dividir");
        cmdDiv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdDivActionPerformed(evt);
            }
        });

        jLabel1.setText("Resultado");

        lblAns.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        lblAns.setText("0");
        lblAns.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        cmdClear.setText("C");
        cmdClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cmdClear))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtFieldB, javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(cmdAdd)
                                    .addGap(18, 18, 18)
                                    .addComponent(cmdSubs)
                                    .addGap(18, 18, 18)
                                    .addComponent(cmdMult)
                                    .addGap(18, 18, 18)
                                    .addComponent(cmdDiv))
                                .addComponent(txtFieldA))
                            .addComponent(lblAns, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtFieldA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(txtFieldB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdAdd)
                    .addComponent(cmdSubs)
                    .addComponent(cmdMult)
                    .addComponent(cmdDiv))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(cmdClear))
                .addGap(18, 18, 18)
                .addComponent(lblAns)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtFieldBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFieldBActionPerformed
	// TODO add your handling code here:
    }//GEN-LAST:event_txtFieldBActionPerformed

    private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAddActionPerformed
	lblAns.setForeground(Color.black);
	lblAns.setText(Controlador.getInstance().ejecutar(Constantes.SUMA,
		txtFieldA.getText(),
		txtFieldB.getText()).toString());
    }//GEN-LAST:event_cmdAddActionPerformed

    private void cmdSubsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSubsActionPerformed
	lblAns.setForeground(Color.black);
	lblAns.setText(Controlador.getInstance().ejecutar(Constantes.RESTA,
		txtFieldA.getText(),
		txtFieldB.getText()).toString());
    }//GEN-LAST:event_cmdSubsActionPerformed

    private void cmdMultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdMultActionPerformed
	lblAns.setForeground(Color.black);
	lblAns.setText(Controlador.getInstance().ejecutar(Constantes.MULTIPLICACION,
		txtFieldA.getText(),
		txtFieldB.getText()).toString());
    }//GEN-LAST:event_cmdMultActionPerformed

    private void cmdDivActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDivActionPerformed
//        String ans = Controlador.getInstance().ejecutar(Constantes.DIVISION,
//                txtFieldA.getText(),
//                txtFieldB.getText()).toString();
//        if (txtFieldB.getText().equals("0")) {
//            lblAns.setForeground(Color.red);
//            lblAns.setText("No se puede dividir entre 0");
//        } else {
//            lblAns.setForeground(Color.black);
//            lblAns.setText(ans);
//        }
	lblAns.setText(Controlador.getInstance().ejecutar(Constantes.DIVISION,
		txtFieldA.getText(), txtFieldB.getText()).toString());
    }//GEN-LAST:event_cmdDivActionPerformed

    private void cmdClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdClearActionPerformed
	txtFieldA.setText("");
	txtFieldB.setText("");
	lblAns.setText("");
    }//GEN-LAST:event_cmdClearActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
	/*
	 * Set the Nimbus look and feel
	 */
	//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
	 * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
	 * feel. For details see
	 * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
	 */
	try {
	    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
		if ("Nimbus".equals(info.getName())) {
		    javax.swing.UIManager.setLookAndFeel(info.getClassName());
		    break;
		}
	    }
	} catch (ClassNotFoundException ex) {
	    java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (InstantiationException ex) {
	    java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (IllegalAccessException ex) {
	    java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (javax.swing.UnsupportedLookAndFeelException ex) {
	    java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	//</editor-fold>

	/*
	 * Create and display the form
	 */
	java.awt.EventQueue.invokeLater(new Runnable() {

	    public void run() {
		new FramePrincipal().setVisible(true);
	    }
	});
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdAdd;
    private javax.swing.JButton cmdClear;
    private javax.swing.JButton cmdDiv;
    private javax.swing.JButton cmdMult;
    private javax.swing.JButton cmdSubs;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblAns;
    private javax.swing.JTextField txtFieldA;
    private javax.swing.JTextField txtFieldB;
    // End of variables declaration//GEN-END:variables
}
